/**
*   表单设计器 列表页
 */
mini.parse();
var formGrid = mini.get("formGrid");
var addWindow = mini.get("addWindow");
var url = bootPATH + "../frame/frame1/pages/";      	//路径前缀
formGrid.setUrl(ajaxUrl + "/formList/list.do");
formGrid.load();
var formDesignAction = [{
    action: 'add',
    formListId: '',
    edit_data: '',
    formName: ''
}];
// $.ajax({
//     url: ajaxUrl + "/formList/list.do",
//     type:"post",
//     success: function (data) {
//         console.log(data);
//     },
//     error: function () {
//         mini.alert('失败')
//     }
// });
var formId = '';
parent.activeChanged();//更新表格页

function delTable() {
    var rows = formGrid.getSelecteds();
    var obj = []
    for(var i = 0; i < rows.length; i++){
    	var o = rows[i]
    	obj.push(o.formListId);
    }
    
    if (rows.length > 0) {
        mini.confirm("确定删除选中记录？","系统提示",function(action) {
            if (action == "ok") {
                console.log(rows,'删除');
                $.ajax({
                    url: ajaxUrl + "/formList/deleteFormList.do",
                    type: "post",
                    dataType: "json",
                    traditional: true,
                    data: {
                    	formListId : obj[0]
                    },
                    success: function(data){
                        console.log(data,'数据');
                        formGrid.reload();
                        mini.alert('删除成功');
                        

                    },
                    error : function(data){
                        mini.alert('删除失败')
                    }
                });
            }
        });
    }else {
        mini.alert('请选择需要删除的行');
    }
}

//跳转到表单设计器
function jumpToFormDesign(act) {

    if(act == 'add') {//新增

        formDesignAction = [{
            action: 'add',
            formListId: '',
            edit_data: '',
            formName: ''
        }];
        // localStorage.clear();
        localStorage.removeItem("formDesignAction");
        localStorage.setItem("formDesignAction",JSON.stringify(formDesignAction));
        var item = {
            iconCls: "fa fa-desktop",
            // id: formListId,
            text: "表单设计器",
            url: "../pages/formDesign/formDesign4.html"
        };
        parent.activeTab(item);//与父页面通信

    }else if (act == 'edit') {
        var row = formGrid.getSelecteds();
        console.log(row);
        if (row.length > 1) {
            mini.alert('不能对多条数据进行编辑');
        } else if (row.length == 0) {
            mini.alert('请选择需要编辑的行');
        } else {
            //编辑
            var formListId = row[0].formListId;
            var formName = row[0].formName;
            var modelId = row[0].model.id;
            $.ajax({
                url: ajaxUrl + "/formltems/findByFormListId.do",
                type: "POST",
                data: {
                    formId: formListId
                },
                success: function (data) {
                    console.log(data,'编辑的数据');
                    formDesignAction = [{//表单设计器
                        action: 'edit',
                        formListId: formListId,
                        modelId: modelId,
                        edit_data: data.data,
                        formName: formName
                    }];
                    // localStorage.clear();
                    localStorage.removeItem("formDesignAction");
                    localStorage.setItem("formDesignAction",JSON.stringify(formDesignAction));
                    var item = {
                        iconCls: "fa fa-desktop",
                        text: "表单设计器",
                        url: "../pages/formDesign/formDesign4.html"
                    };
                    parent.activeTab(item);//与父页面通信
                },
                error : function(data){
                }
            });
        }
    }
}
//流程操作
function onActionRenderer(e) {
    var grid = e.sender;
    var record = e.record;
    var uid = record._uid;
    var rowIndex = e.rowIndex;

    var s = ' <a class="mini-button Edit_Button" href="javascript:processVersionControl(\'' + uid + '\')" >流程版本控制</a>'
    		+ ' <a class="mini-button Edit_Button" href="javascript:addActiviti(\'' + uid + '\')" >创建新流程</a>'
    		+ ' <a class="mini-button Edit_Button" href="javascript:relationActiviti_(\'' + uid + '\')" >关联已有流程</a>'
    		+ ' <a class="mini-button Edit_Button" href="javascript:editActiviti(\'' + uid + '\')" >编辑流程</a>'
    		+ ' <a class="mini-button Edit_Button" href="javascript:releaseActiviti(\'' + uid + '\')" >发布流程</a>'
    		+ ' <a class="mini-button Edit_Button" href="javascript:removeActiviti(\'' + uid + '\')" >删除当前流程</a>'
    		+ ' <a class="mini-button Edit_Button" href="javascript:seeActivitiImg(\'' + uid + '\')" >查看流程图</a>';
    return s;
}
////打开审批页面
//mini.open({
//    url: url +"activiti/activiti_form_list.html",
//    title: "流程参照",
//    width: 600,
//    height: 500,
//    onload: function () {
//        var iframe = this.getIFrameEl();
//        var data = {
//            action: "relation",
//            formListId : row.formListId
//        };
//        iframe.contentWindow.SetData(data);
//    },
//    ondestroy: function (action) {
//    	formGrid.reload();
//    }
//});
var rowId;
//流程版本控制
function processVersionControl(row_uid){
	var row = formGrid.getRowByUID(row_uid);
	console.log(row);
	$.ajax({
		url: '',
		type: 'POST',
		success: function(data){
			console.log(data);
			if (data.type) {
				mini.open({
		            url: url +"activiti/datagridDetail/processVersionControl.html",
		            title: "流程历史版本", width: 600, height: 400,
		            onload: function () {
		                var iframe = this.getIFrameEl();
		                var data = { action: "edit", id: row.modularId };
		                iframe.contentWindow.SetData(data);
		            },
		            ondestroy: function (action) {
		            	console.log(action);
		            }
		        });
			} else {
		        mini.alert("1111");
		    }    
		},
		error: function(data){
			console.log('请求失败');
		}
	})
}
//创建新流程
function addActiviti(row_uid) {
    var row = formGrid.getRowByUID(row_uid);
    if(!row.processId){
    	//名字，id
        addWindow.show();
        rowId = row.formListId;
    }else{
    	mini.alert('当前已有流程');
    }
}
//创建保存
function establish(){
	var modelName = mini.getByName('modelName');
	var processName = modelName.getValue();
	if(processName){
		$.ajax({
	    	url: ajaxUrl + "/activitiFormController/create.do",
	        type:"post",
	        data: {
	        	formId: rowId,
	        	processName: processName
	        },
	        success: function (data) {
	        	if(data.type == 'sucess'){
	        		var id = data.modelId;
		            window.location.href = ajaxUrl + "/modeler.html?modelId="+id;
	        	}else{
	        		addWindow.hide();
	        		mini.alert(data.msg);
	        	}
	        },
	        error: function () {
	            mini.alert("失败");
	        }
	    })
	}else{
		mini.alert('请输入名称');
	}
}
//取消
function cancelRow(){
	addWindow.hide();
}
//关联已有流程
function relationActiviti_(row_uid) {
	//弹出窗口路径前缀
	var url = bootPATH + "../frame/frame1/pages/";
    var row = formGrid.getRowByUID(row_uid);
    if (!row.processId) {
    	//打开审批页面
        mini.open({
            url: url +"activiti/activiti_form_list.html",
            title: "流程参照",
            width: 600,
            height: 500,
            onload: function () {
                var iframe = this.getIFrameEl();
                var data = {
                    action: "relation",
                    formListId : row.formListId
                };
                iframe.contentWindow.SetData(data);
            },
            ondestroy: function (action) {
            	formGrid.reload();
            }
        });
    }else{
    	mini.alert('当前已有流程');
    }
}
//编辑流程
function editActiviti(row_uid) {
    var row = formGrid.getRowByUID(row_uid);
    if(row.processId){
    	var processId = row.processId;
    	var processName = row.processName;
    	window.location.href = ajaxUrl + "/modeler.html?modelId="+processId;
    }else{
    	mini.alert('当前无流程,请先创建模型');
    }
}
//发布
function releaseActiviti(row_uid) {
    var row = formGrid.getRowByUID(row_uid);
    var processId = row.processId;
	var formListId = row.formListId;
//	//打开审批页面
//    mini.open({
//        url: url +"activiti/datagridDetail/releaseActiviti.html",
//        title: "流程发布",
//        width: 360,
//        height: 200,
//        onload: function () {
//            var iframe = this.getIFrameEl();
//            var data = {
//                action: "relation",
//                formListId: formListId,
//                processId : processId
//            };
//            iframe.contentWindow.SetData(data);
//        },
//        ondestroy: function (action) {
//        	console.log(action);
//        	//formGrid.reload();
//        }
//    });
    if(row.processId){
    	var processId = row.processId;
    	var formListId = row.formListId;
    	$.ajax({
	    	url: ajaxUrl + "/activitiFormController/deploy.do",
	        type:"post",
	        data: {
	        	formId: formListId,
	        	modelId: processId
	        },
	        success: function (data) {
	        	console.log(data);
	        	if(data.type == "error"){
	        		mini.alert(data.msg + ',请先编辑流程')
	        	}else{
	        		mini.alert(data.msg);
		        	formGrid.reload();
	        	}
	        },
	        error: function () {
	            mini.alert("失败");
	        }
	    })
    }else{
    	mini.alert('当前无流程,请先创建模型');
    }
}
//删除当前流程
function removeActiviti(row_uid){
	var row = formGrid.getRowByUID(row_uid);
	if(row.processId){
		mini.confirm('确定删除当前流程','系统提示',function(action){
			if(action == 'ok'){
				var formListId = row.formListId;
		    	$.ajax({
			    	url: ajaxUrl + "/activitiFormController/deleteProcessId.do",
			        type:"post",
			        data: {
			        	formListId: formListId
			        },
			        success: function (data) {
			        	console.log(data);
			        	if(data.type == 'success'){
			        		formGrid.reload();
			        	}else{
			        		mini.alert(data.msg);
			        	}
			        },
			        error: function () {
			            mini.alert("失败");
			        }
			    })
			}
		})
    }else{
    	mini.alert('当前无流程,请先创建模型');
    }
}
//查看流程图
function seeActivitiImg(row_uid) {
    var row = formGrid.getRowByUID(row_uid);
    console.log(row);
    if (row.processId) {
    	var formListId = row.formListId;
        fileUrl = ajaxUrl + "/activitiFormController/showView.do?formId="+formListId;
        images = document.getElementById("imgs")
        images.src = fileUrl;
        var process = document.getElementById("process");
        console.log(process);
        process.style.display = "block";
    }else{
    	mini.alert('当前无流程');
    }
}
//关闭流程图
function close(){
	process.style.display = "none";
}
//取消创建模型
function cancelRow() {
	addWindow.hide();
}
//刷新
function refresh(){
	formGrid.reload();
}